package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.object.pojo.dto.RevenueDTO;
import com.tbit.uqbike.object.pojo.vo.RevenueGroupDayVO;
import com.tbit.uqbike.object.pojo.vo.RevenueVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.RevenueService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 财务统计-营收统计
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-10
 */
@RestController
@RequestMapping("/revenue")
public class RevenueController {

    @Resource
    private RevenueService revenueService;


    /**
     * 获取营收统计
     * @param dto dto
     * @return Result
     */
    @GetMapping("/getRevenue")
    public Result<Object> getRevenue(RevenueDTO dto) {
        if (Objects.isNull(dto.getType()) || Objects.isNull(dto.getBrandId()) || Objects.isNull(dto.getStartTime()) || Objects.isNull(dto.getEndTime())){
            return Result.error("参数错误");
        }
        return revenueService.getRevenue(dto);
    }


    /**
     * 导出营收统计
     * @param dto dto
     * @return Rsult
     */
    @GetMapping("/exportRevenue")
    public Result<Object> exportRevenue(RevenueDTO dto) {
        if (Objects.isNull(dto.getType()) || Objects.isNull(dto.getBrandId()) || Objects.isNull(dto.getStartTime()) || Objects.isNull(dto.getEndTime())){
            return Result.error("参数错误");
        }
        Object data = revenueService.getRevenue(dto).getData();
        JsonResult jsonResult;
        if (0 == dto.getType()){
            ExcelUtil<RevenueVO> excel = new ExcelUtil<>(RevenueVO.class);
            jsonResult = excel.exportExcel((List<RevenueVO>)data, "营收统计(汇总)");
        }else {
            ExcelUtil<RevenueGroupDayVO> excel = new ExcelUtil<>(RevenueGroupDayVO.class);
            jsonResult = excel.exportExcel((List<RevenueGroupDayVO>)data, "营收统计(按天)");
        }
        return Result.success(jsonResult.getData());
    }


}
